<%@ include file="/init.jsp"%>
<%@page import="com.liferay.portal.theme.ThemeDisplay"%>
<%@page import="com.crossriver.crgis.database.service.persistence.Application_C_of_OUtil"%>
<!-- Language -->
<%@ page import="com.liferay.portal.kernel.language.LanguageUtil" %>

<%@ page
	import="com.crossriver.crgis.database.model.Supporting_Document"%>
<%@page
	import="com.crossriver.crgis.database.model.Application_C_of_O"%>
<%@ page
	import="com.crossriver.crgis.cofo.util.ApplicationCofOConstants"%>

<%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ include file="/init.jsp"%>

<liferay-portlet:actionURL name="submitSupportingDoc"
	var="submitSupportingDoc"></liferay-portlet:actionURL>

<%
	Application_C_of_O appl = (Application_C_of_O) request
			.getAttribute(ApplicationCofOConstants.APPL_C_OF_O_SESSION_ATTR);


Application_C_of_O applicatn = 
	Application_C_of_OUtil.findByPrimaryKey(appl.getApplicationId());
%>
<%
	List<Supporting_Document> suppDocs = (List<Supporting_Document>) request.getAttribute("suppDoc");
	int docCounter = suppDocs.size();
	
	List<Supporting_Document>  suppsError = (List<Supporting_Document>) request.getAttribute("suppError");
%>


<%
if( null != suppsError){
for (int k=0; k<suppsError.size(); k++){
	Supporting_Document suppError = suppsError.get(k);
%>
<liferay-ui:error key="file_too_large" message='<%= LanguageUtil.format(pageContext, "file_too_large", suppError.getDocumentUpload()) %>'  />

<%
}
}
%>
<div id="errorMessage">

</div>
<form action="<%=submitSupportingDoc.toString()%>"	enctype="multipart/form-data" method="post"	name="submitSupportingDoc_fm">
	 <% String app = applicatn.getApplication(); %>
 <div id="main">
	<%
	if("certification".equalsIgnoreCase(app)){
	%>
	<h1>Application for Certificate Of Occupancy</h1>
	<%
	}else if("re-certification".equalsIgnoreCase(app)){
	%>
	<h1>Application for Re-Certificate Of Occupancy</h1>
	<%
	}else {
	%>
	<h1>Application for State Land</h1>
	<%
	}
	%>

	<div class="content">
		<table width="100%" border="0" cellpadding="0" cellspacing="0">
			<tr>
			<td width="15%" valign="top">
				<aui:input name="applicationId" type="hidden" value="<%= appl.getApplicationId() %>"></aui:input>
				<aui:input name="buttonClicked" type="hidden" value=""></aui:input>
				<table width="100%" id="steps" cellpadding="0" cellspacing="0">
       		<tr><td valign="middle" align="center">STEP 1</td></tr>
       		<tr><td valign="middle" align="center">STEP 2</td></tr>
	        <tr><td valign="middle" align="center">STEP 3</td></tr>
          <tr><td valign="middle" align="center">STEP 4</td></tr>
          <tr><td valign="middle" align="center" class="current">STEP 5</td></tr>
          <tr><td valign="middle" align="center">STEP 6</td></tr>
          <tr><td valign="middle" align="center">STEP 7</td></tr>
          <tr><td valign="middle" align="center">STEP 8</td></tr>                               
       	</table>
			</td>
			<td width="85%" valign="top">
				<table width="100%" id="mainform">
					<tr>
						<td class="title"> Supporting Documents <aui:input name="docsCounter" type="hidden" value="<%=docCounter %>"></aui:input></td>
					</tr>
					<tr>
						<td colspan="3" align="right">
							<hr>
							<aui:button type="button" value="Exit Application" onClick="exitApplication();"></aui:button>
             	<aui:button type="submit" value="Save" onClick="setButtonClicked('btnSave');" ></aui:button>
            	<aui:button type="submit" value="Save and Continue" onClick="setButtonClicked('btnSaveAndContinue');" ></aui:button>
             	<aui:button type="button" value="Cancel Application" onClick="confirmPost()"></aui:button>
						</td>
					</tr>
					
					<tr>
						<td colspan="3" align="left">
						  <strong>
						  	<br/>
						  	Please ensure that the size of each of the files you are uploading is not more than 450Kb.
						  </strong>
						</td>
					</tr>

					<%
						for (int i = 1; i <= suppDocs.size(); i++) {
							Supporting_Document supDoc = suppDocs.get(i - 1);
					%>


					<tr>
						<td colspan="3"><br>
						<span id="label"><%=supDoc.getDocumentName()%></span> <input id="fileName_<%=i%>"
							name="fileName_<%=i%>" type="file"></td>
							</tr>
					<%
						}
					%>

					<tr>
						<td colspan="3"><br>
							<hr>
							<p>Please, Kindly ensure that this form is properly filled and all information provided here are accurate. All fields asterisk in
							"red" are compulsory, as the form would not be processed if the	fields are left empty.</p>
						</td>
					</tr>
					<tr>
						<td colspan="3" align="right">
							<input type="hidden" value="<%=suppDocs.size()  %>" id="fileCount">
							<aui:button type="button" value="Exit Application" onClick="exitApplication();"></aui:button>
             	
              
             	<aui:button type="submit" value="Save" onClick="setButtonClicked('btnSave');" ></aui:button>
            	<aui:button type="submit" value="Save and Continue" onClick="setButtonClicked('btnSaveAndContinue');" ></aui:button>
             	<aui:button type="button" value="Cancel Application" onClick="confirmPost()"></aui:button>
						</td>
					</tr>
				</table>
			</td>
			</tr>
		</table>
	</div>
</div>
</form>


	<liferay-portlet:actionURL name="exitApplication" var="exitApplication"></liferay-portlet:actionURL>
	<form name="<portlet:namespace/>exitApplication_fm" method="post"	id="<portlet:namespace/>exitApplication_fm" action="<%=exitApplication.toString()%>"></form>
	<script type="text/javascript">
	<!--
	function exitApplication()
	{
		document.forms["<portlet:namespace/>exitApplication_fm"].submit();
	}
	//-->
	</script>

<script type="text/javascript">
<!--
	function confirmPost()
	{
	var agree=confirm("Cancelling the process will delete the record you have entered into the portal for this application. Do you want to proceed?");
	if (agree){
		document.forms["<portlet:namespace/>deleteApplication_fm"].submit();
		return true ;
	}
	else{
		return false ;
	}
	}
//-->
</script>
<script type="text/javascript">
 
 var errorMessage = '';
 	function submitForm(){
 		
 		if(getFileSize()){
 			document.forms["submitSupportingDoc_fm"].submit();
 		}else{
 			
 			var errorDiv = document.getElementById("errorMessage");
 			errorDiv.innerHTML = errorMessage;
 			
 		}
 		
 		
 	}
 
	function getFileSize()
		{
		
		errorMessage = '';
			try
			
		    {
				
				var fileCount = document.getElementById("fileCount").value;
				
				 if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
					 
					  var myFSO = new ActiveXObject("Scripting.FileSystemObject");	
					  var sizeError = false;
					  
					  for (i = 1; i <= fileCount; i++) {
						var fileName = "fileName_" +i;
											 	
					 	var filepath = document.getElementById(fileName).value;  
					 	
						var thefile = myFSO.getFile(filepath);
						if(null != thefile){
							
							var size = thefile.size;
							
							if(thefile.size != null){
								if (size>500000){

									errorMessage =   errorMessage + "[" +filepath +  "] file too large: " + size + " KB" + "<br/>";
									sizeError = true;
								}
								else{
								}
					   	    	
							}
						}
						
					}
					  
					  if(sizeError == true){
							return false;
						}else{
							return true;
						}
				 }
				else
					{
					var sizeError = false;
					for ( i = 1; i <= fileCount; i++) {
						var fileName = "fileName_" +i;
					    fileupload = document.getElementById(fileName);  
					   	if(fileupload.files)
							{
					   	
					   		var size = 0; 
					   	    if(fileupload.files.item(0) != null){
					   	    	size = fileupload.files.item(0).size/1024;
					   	    	
								if (size>500000){
									errorMessage =   errorMessage + "[" +fileupload.files.item(0).fileName +  "] file too large: " + size + " KB" + "<br/>";
									sizeError = true;
								}
								else{
								}
					   	    }
							
						} 
					}
					
					if(sizeError == true){
						return false;
					}else{
						return true;
					}
					
				} 
				 
			}	
		    catch(err)
		    {
		    	//if the browser is IE and the ActiveX is not enabled, error will be thrown
		    	//we are seeing as normal since most of the browsers will be IE based. The request will be
		    	//submitted and the validation will be done at the back end.
		        return true;
		   }
		}
	
	
</script>
<liferay-portlet:actionURL name="deleteApplication" var="deleteApplication"></liferay-portlet:actionURL>
 <aui:form action="<%=deleteApplication.toString() %>" method="post" name="deleteApplication_fm">
  	<aui:input name="applicationId" type="hidden" value="<%=appl.getApplicationId() %>"></aui:input>
 </aui:form>
 
 